package com.sit.estatemanagementserver.dao;

import com.sit.estatemanagementserver.enity.Image;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class ImageDao {

    @Autowired
    private DatabaseManager db;

    // 无需重构, Image实体未变
    private Image resultSetToImage(ResultSet rs) throws SQLException {
        return new Image(
                rs.getString(DatabaseContract.ImageEntry.COLUMN_IMAGE_ID),
                rs.getString(DatabaseContract.ImageEntry.COLUMN_IMAGE_PATH)
        );
    }

    public Image getImageById(String imageId) {
        String sql = "SELECT * FROM " + DatabaseContract.ImageEntry.TABLE_NAME + " WHERE " +
                DatabaseContract.ImageEntry.COLUMN_IMAGE_ID + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, imageId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return resultSetToImage(rs);
                }
            }
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error getting image '" + imageId + "': " + e.getMessage());
        }
        return null;
    }

    public boolean addImage(Image image) {
        String sql = "INSERT INTO " + DatabaseContract.ImageEntry.TABLE_NAME + " (" +
                DatabaseContract.ImageEntry.COLUMN_IMAGE_ID + ", " +
                DatabaseContract.ImageEntry.COLUMN_IMAGE_PATH + ") VALUES (?, ?)";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, image.getImageId());
            pstmt.setString(2, image.getImagePath());
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error adding image '" + image.getImageId() + "': " + e.getMessage());
            return false;
        }
    }

    public boolean updateImage(Image image) {
        String sql = "UPDATE " + DatabaseContract.ImageEntry.TABLE_NAME + " SET " +
                DatabaseContract.ImageEntry.COLUMN_IMAGE_PATH + " = ? WHERE " +
                DatabaseContract.ImageEntry.COLUMN_IMAGE_ID + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, image.getImagePath());
            pstmt.setString(2, image.getImageId());
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error updating image '" + image.getImageId() + "': " + e.getMessage());
            return false;
        }
    }

    public boolean deleteImage(String imageId) {
        String sql = "DELETE FROM " + DatabaseContract.ImageEntry.TABLE_NAME + " WHERE " +
                DatabaseContract.ImageEntry.COLUMN_IMAGE_ID + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, imageId);
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error deleting image '" + imageId + "': " + e.getMessage());
            return false;
        }
    }
}